package com.example.demo.runner;

import com.example.demo.properties.BaseInfoProps;
import com.example.demo.properties.ProjectBaseMsgProps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Component
@Order(9)
public class ProjectBaseMsgRunner implements ApplicationRunner {

    @Autowired
    private ProjectBaseMsgProps props;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String port = props.getPort();

        String contextPath = props.getContextPath();

        // 项目启动后执行该方法

        try {

            // 获取本机内网ip
            String ip = InetAddress.getLocalHost().getHostAddress();
            if (ip.indexOf("127.0.0.1") <= -1) {
                ip = "127.0.0.1";
            }
            String address = "http://" + ip + ":" + port + contextPath;
            System.out.println("访问地址:" + address);
            System.out.println("swagger2管理页面访问地址:" + address + "/swagger-ui.html");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
